package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    @Transactional
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);

        //只能查询自己的购物车数据
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);

        //判断当前商品是否在购物车中
        if (shoppingCarts != null && shoppingCarts.size() == 1) {
            shoppingCart = shoppingCarts.get(0);
            //如果已经存在，就更新数量，数量加1
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.update(shoppingCart);
        } else {
            //如果不存在，插入数据，数量就是1
            shoppingCart.setNumber(1);
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                //如果添加到购物车的是菜品，查出菜品的需要补充的冗余数据填充进去
                Dish dish = dishMapper.findDishById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            } else {
                //如果添加到购物车的是套餐，查出套餐的需要补充的冗余数据填充进去
                SetmealVO setmealVO = setmealMapper.selectSetMealById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setImage(setmealVO.getImage());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);

        }

    }

    /**
     * 查看购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public List<ShoppingCart> list(ShoppingCart shoppingCart) {
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.cleanShoppingCartByUserId(BaseContext.getCurrentId());
    }

    @Override
    @Transactional
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        log.info("小程序的减号功能，接收到的参数为{}",shoppingCartDTO);
        //当菜品或者套餐的数量为1时，按减号应该把这条购物车删除

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
        shoppingCart = shoppingCarts.get(0);
        if (shoppingCart.getNumber() == 1){
            log.info("餐品数量为0，删除购物车中的记录！");
            shoppingCartMapper.deleteIfNumZero(shoppingCart.getId());
        }
        else {
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.update(shoppingCart);
        }
    }
}
